<?php

namespace app\api\middleware;


use think\Request;
use think\facade\Cache;
use think\facade\Db;
/**
 * 检测用户登录状态
 * Class CheckAdmin
 * @package app\admin\middleware
 */
class CheckAdmin
{

    use \app\common\traits\Crypt;

    public function handle(Request $request, \Closure $next)
    {
        /**
         * 拼团订单失效
         */
        $order_model =  app('app\common\model\mall\Order');
        $order =  app('app\api\controller\Order');
        $group_master =  app('app\common\model\mall\GroupMaster');
        $group_join =  app('app\common\model\mall\GroupJoin');
        $order_list =  $group_master->where(['is_over'=>0])->whereTime('end_time','<',time())->column('id');
        $account =  app('app\common\model\mall\Account');
        $account_item =  app('app\common\model\media\AccountItem');
        $configuration =  app('app\common\model\system\Configuration');
        $integral =  app('app\common\model\system\Integral');

        /*if($order_list) {
             foreach($order_list as $k=>$v){
                 $order->refund($v);
             }
         };*/
        /*
         * 15分钟未付款订单失效
         *
         */
         $no_pay = $order_model->where('status',7)->field('id,create_time')->select()->toArray();
        if($no_pay){
             foreach($no_pay as $k=>$v){

                 if(time() - strtotime($v['create_time']) >= 900){
                     $order = $order_model->where('id',$v['id'])->find();
                     $order->save(['status'=>6]);
                     $account->where('id',$order->goods_id)->inc('alone_number',1)->update();
                     $account_item->where('id',$order->sku_id)->inc('number',1)->update();
                 }
             }
         }
        //dd($order_list);

        $wx_user = app('app\common\model\user\WxUsers');
        //验证失败
        if ((!$request->param('key') || $request->param('key') == "undefined" || !Cache::get('Rmt_' . $this->decrypt($request->param('key')))) && !$request->param('is_district')) { //强制登录
            //根据键名获取键值
            return json(['code' => 400, 'msg' => '登录异常,请重新登录']);
        } elseif ($request->param('is_district') == 1) { //非强制登陆
            $request->user_id = 0;
            return $next($request);
        };
        $phone = $this->decrypt($request->param('key'));

        $user = $wx_user->where('phone', $phone)->find();
        if (!$user) {
            return json(['code' => 400, 'msg' => '登录异常,请重新登录']);
        }
        $request->user_id = $user->id;

        //获取用户会员卡折扣状态
        $cost = $configuration->where('id',146)->value('val');

        $request->user_cost = floatval($cost/10);
        //判断积分等级

        /*$integral_list = $integral->order('id desc')->select()->toArray();
        foreach($integral_list as $k=>$v){
            if($user->cumulative_integral> $v['min']){
                $user_rank = $v['id'];
                break;
            }else{
                continue;
            }
        }*/

        //持久化登录态
        Cache::set('Rmt_' . $phone, $user->api_token, 36000000);
        //$request->user_id = 60;
        //验证成功
        return $next($request);
    }

}